package org.xwalk.core.internal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class XWalkExternalExtensionManagerBridge extends XWalkExternalExtensionManagerInternal {
    private XWalkCoreBridge coreBridge;
    private Object wrapper;

    public Object getWrapper() {
        return wrapper;
    }



    public XWalkExternalExtensionManagerBridge(XWalkViewBridge view, Object wrapper) {
        super(view);

        this.wrapper = wrapper;
        reflectionInit();

    }

        @Override
    public Activity getViewActivity() {
        if (getViewActivityMethod == null || getViewActivityMethod.isNull()) {
            return getViewActivitySuper();
        } else {
            return (Activity)getViewActivityMethod.invoke();
        }
    }

    public Activity getViewActivitySuper() {
        Activity ret;
        ret = super.getViewActivity();
        if (ret == null) return null;
        return ret;
    }

    private ReflectMethod getViewActivityMethod = new ReflectMethod(null, "getViewActivity");

    @Override
    public Context getViewContext() {
        if (getViewContextMethod == null || getViewContextMethod.isNull()) {
            return getViewContextSuper();
        } else {
            return (Context)getViewContextMethod.invoke();
        }
    }

    public Context getViewContextSuper() {
        Context ret;
        ret = super.getViewContext();
        if (ret == null) return null;
        return ret;
    }

    private ReflectMethod getViewContextMethod = new ReflectMethod(null, "getViewContext");

    @Override
    public void loadExtension(String extensionPath) {
        if (loadExtensionStringMethod == null || loadExtensionStringMethod.isNull()) {
            loadExtensionSuper(extensionPath);
        } else {
            loadExtensionStringMethod.invoke(extensionPath);
        }
    }

    public void loadExtensionSuper(String extensionPath) {
        super.loadExtension(extensionPath);
    }

    private ReflectMethod loadExtensionStringMethod = new ReflectMethod(null, "loadExtension");

    @Override
    public void onStart() {
        if (onStartMethod == null || onStartMethod.isNull()) {
            onStartSuper();
        } else {
            onStartMethod.invoke();
        }
    }

    public void onStartSuper() {
        super.onStart();
    }

    private ReflectMethod onStartMethod = new ReflectMethod(null, "onStart");

    @Override
    public void onResume() {
        if (onResumeMethod == null || onResumeMethod.isNull()) {
            onResumeSuper();
        } else {
            onResumeMethod.invoke();
        }
    }

    public void onResumeSuper() {
        super.onResume();
    }

    private ReflectMethod onResumeMethod = new ReflectMethod(null, "onResume");

    @Override
    public void onPause() {
        if (onPauseMethod == null || onPauseMethod.isNull()) {
            onPauseSuper();
        } else {
            onPauseMethod.invoke();
        }
    }

    public void onPauseSuper() {
        super.onPause();
    }

    private ReflectMethod onPauseMethod = new ReflectMethod(null, "onPause");

    @Override
    public void onStop() {
        if (onStopMethod == null || onStopMethod.isNull()) {
            onStopSuper();
        } else {
            onStopMethod.invoke();
        }
    }

    public void onStopSuper() {
        super.onStop();
    }

    private ReflectMethod onStopMethod = new ReflectMethod(null, "onStop");

    @Override
    public void onDestroy() {
        if (onDestroyMethod == null || onDestroyMethod.isNull()) {
            onDestroySuper();
        } else {
            onDestroyMethod.invoke();
        }
    }

    public void onDestroySuper() {
        super.onDestroy();
    }

    private ReflectMethod onDestroyMethod = new ReflectMethod(null, "onDestroy");

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (onActivityResultintintIntentMethod == null || onActivityResultintintIntentMethod.isNull()) {
            onActivityResultSuper(requestCode, resultCode, data);
        } else {
            onActivityResultintintIntentMethod.invoke(requestCode, resultCode, data);
        }
    }

    public void onActivityResultSuper(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    private ReflectMethod onActivityResultintintIntentMethod = new ReflectMethod(null, "onActivityResult");

    @Override
    public void onNewIntent(Intent intent) {
        onNewIntentIntentMethod.invoke(intent);
    }


    private ReflectMethod onNewIntentIntentMethod = new ReflectMethod(null, "onNewIntent");


    void reflectionInit() {
        coreBridge = XWalkCoreBridge.getInstance();
        if (coreBridge == null) return;

        getViewActivityMethod.init(wrapper, null,
                "getViewActivity");
        getViewContextMethod.init(wrapper, null,
                "getViewContext");
        loadExtensionStringMethod.init(wrapper, null,
                "loadExtension", String.class);
        onStartMethod.init(wrapper, null,
                "onStart");
        onResumeMethod.init(wrapper, null,
                "onResume");
        onPauseMethod.init(wrapper, null,
                "onPause");
        onStopMethod.init(wrapper, null,
                "onStop");
        onDestroyMethod.init(wrapper, null,
                "onDestroy");
        onActivityResultintintIntentMethod.init(wrapper, null,
                "onActivityResult", int.class, int.class, Intent.class);
        onNewIntentIntentMethod.init(wrapper, null,
                "onNewIntent", Intent.class);
    }
}
